package hk.polyu;

import java.io.*;
import java.util.*;
import java.util.AbstractMap.SimpleEntry;

public class app {
	
	
	public static void main(String[] args) {
		//testCraw();
		//testSearch();
		testBuildIndex();
		System.out.println("Done");
	}
	
	static void testCraw() {
		
		String p = "/Users/hezongjian/Desktop/temp/";
		String u = "http://en.wikipedia.org/wiki/Category:IPad";

		WikiCrawler wc = new WikiCrawler(u, p);
		
		wc.setOnCrawlListener(new WikiCrawler.OnCrawlListener() {
			@Override
			public void onPageCrawled(SimpleEntry<String, Integer> item) {
				System.out.printf("Fetching item %s at level %d\r\n", item.getKey(), item.getValue());
			}

			@Override
			public void onCrawlComplete() {
				System.out.println("Crawl Complete");
			}
		});
		
		//String item = "<a class=\"CategoryTreeLabel  CategoryTreeLabelNs14 CategoryTreeLabelCategory\" href=\"/wiki/Category:IPad_games\">IPad games</a>";
		//wc.appendQueue(item, 2);
		wc.crawl();
		
	}
	
	
	static void testSearch(){
		String indexPath = "/Users/hezongjian/Desktop/";
		DocumentIndex di = new DocumentIndex();
		InvertedIndex ii = new InvertedIndex();
		
		try {
			di.load(indexPath + DocumentIndex.FILE_NAME);
			ii.load(indexPath + InvertedIndex.FILE_NAME);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		Searcher q = new Searcher(ii, di);
		
		Vector<QueryResult> finalResult = q.query("\'data transmission' AND \'Data Compression\' OR communication");
		//Vector<QueryResult> finalResult = q.query("communication");
		//Vector<QueryResult> finalResult = q.query("USA");
		Iterator<QueryResult> it = finalResult.iterator();
		while(it.hasNext()) {
			QueryResult r = it.next();
			System.out.printf("%s, w = %f\r\n", r.mName, r.mWeight);
		}
		
	}
	
	static void testBuildIndex() {
		CollectionBuilder cb = new CollectionBuilder("/Users/hezongjian/Desktop/temp");
		//CollectionInfo ci = cb.build(true, "/Users/hezongjian/Desktop/stopword.txt");
		CollectionInfo ci = cb.build(true, null);
		
		DocumentIndex di = new DocumentIndex();
		InvertedIndex ii = new InvertedIndex();
		
		String indexPath = "/Users/hezongjian/Desktop/";  
		
		di.generate(ci);
		ii.generate(ci);
		try {
			di.save(indexPath);
			ii.save(indexPath);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
